<?php
class Login extends Controller
{
	function Login()
	{
		parent::Controller();
		$this->load->library('session');
		$this->load->helper('url');
		$this->load->database();
	}
	function index()
	{
		if(count($_POST) < 2)
		{
			echo '<base href="' . base_url() . '"/>';
			$this->load->view('admin/login');
		}
		else
		{
			$this->db->where('taikhoan', $this->input->post('txtUsername'));
			$this->db->where('matkhau', md5($this->input->post('txtPassword')));
			$query = $this->db->get('tbluser');
			if($query->num_rows() == 0)
			{
				echo '<base href="' . base_url() . '"/>';
				$this->load->view('admin/login', array('error'=> 'Tài khoản hoặc mật khẩu không chính xác'));
			}
			else
			{
				foreach($query->result() as $row)
				{
					$id = $row->id;
					$taikhoan = $row->taikhoan;
					$hoten = $row->hoten;
					$quyen = $row->quyen;
					$active = $row->active;
				}
				if($active)
				{
					$this->session->set_userdata(array('id_thanhvien'=>$id, 'taikhoan'=>$taikhoan, 'quyen'=>$quyen, 'hoten'=>$hoten));
					redirect('admin/quanly');
				}
				else
				{
					echo '<base href="' . base_url() . '"/>';
					$this->load->view('admin/login', array('error'=> 'Tài khoản đã bị khóa'));
				}
			}
		}
	}
}
?>